<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.HisiPHP.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\common\service;

use app\common\service\Base;

class TTaskLog extends Base
{

	/**
	 * 添加任务日志
	 *
	 * @author King
	 * @date 2020年8月21日
	 *
	 * @param integer $relateId	关联ID
	 * @param integer $type		类型（1任务，2评论，3进度）
	 * @param string $remark	备注
	 */
	public function addLog($relateId, $type, $remark) {
		$projectId = 0;
		$taskId = 0;
		$commentId = 0;
		$progressId = 0;
		if ($type == 3) {
			$ttpInfo = db('t_task_progress')->where('id', $relateId)->findOrEmpty();
			$projectId = arrayGet($ttpInfo, 'project_id', 0);
			$taskId = arrayGet($ttpInfo, 'task_id', 0);
			$progressId = $relateId;
		} elseif($type == 2) {
			$ttcInfo = db('t_task_comment')->where('id', $relateId)->findOrEmpty();
			$projectId = arrayGet($ttcInfo, 'project_id', 0);
			$taskId = arrayGet($ttcInfo, 'task_id', 0);
			$commentId = $relateId;
		} else {
			$ttInfo = db('t_task')->where('id', $relateId)->findOrEmpty();
			$projectId = arrayGet($ttInfo, 'project_id', 0);
			$taskId = $relateId;
		}
		if (empty($progressId)|| empty($taskId)|| !in_array($type, [1,2,3])) {
			return false;
		}

		$insData = [
			'project_id' => $projectId,
			'task_id' => $taskId,
			'comment_id' => $commentId,
			'progress_id' => $progressId,
			'type' => $type,
			'remark' => $remark,
			'admin_id' => ADMIN_ID,
			'create_time' => time(),
		];
		return db('t_task_log')->insertGetId($insData);
	}
}